package flc.ast.adapter;


import android.view.View;
import android.widget.ImageView;

import androidx.annotation.NonNull;

import com.blankj.utilcode.util.ConvertUtils;
import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.TimeUtils;
import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.provider.BaseItemProvider;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;

import java.text.SimpleDateFormat;

import flc.ast.R;
import stark.common.basic.adaptermutil.StkProviderMultiAdapter;
import stark.common.basic.adaptermutil.StkSingleSpanProvider;
import stark.common.basic.utils.MediaUtil;
import stark.common.basic.utils.TimeUtil;

public class FileAdapter extends StkProviderMultiAdapter<String> {

    public FileAdapter() {
        addItemProvider(new StkSingleSpanProvider(126));
        addItemProvider(new FileProvider());
    }

    private class FileProvider extends BaseItemProvider<String> {
        @Override
        public int getItemViewType() {
            return StkProviderMultiAdapter.TYPE_VIEW;
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_file;
        }

        @Override
        public void convert(@NonNull BaseViewHolder baseViewHolder, String bean) {
            if (baseViewHolder.getBindingAdapterPosition() == 0) {
                baseViewHolder.getView(R.id.line).setVisibility(View.GONE);
            } else {
                baseViewHolder.getView(R.id.line).setVisibility(View.VISIBLE);
            }
            if (FileUtils.isDir(bean)) {
                baseViewHolder.getView(R.id.llFileItemFolder).setVisibility(View.VISIBLE);
                baseViewHolder.getView(R.id.llFileItemVideo).setVisibility(View.GONE);
                baseViewHolder.setText(R.id.tvFileItemFolderName, FileUtils.getFileName(bean));
            } else {
                baseViewHolder.getView(R.id.llFileItemFolder).setVisibility(View.GONE);
                baseViewHolder.getView(R.id.llFileItemVideo).setVisibility(View.VISIBLE);
                Glide.with(getContext()).load(bean)
                        .into((ImageView) baseViewHolder.getView(R.id.ivFileItemImg));
                baseViewHolder.setText(R.id.tvFileItemVideoName, FileUtils.getFileName(bean));
                String date = TimeUtils.millis2String(FileUtils.getFileLastModified(bean), new SimpleDateFormat("yyyy年MM月dd日"));
                String size = ConvertUtils.byte2FitMemorySize(FileUtils.getFileLength(bean), 1);
                String extension = FileUtils.getFileExtension(bean);
                baseViewHolder.setText(R.id.tvFileItemInfo, date + "，" + size + "，" + extension);
            }
        }
    }

}
